import requests
import json
import pymongo

# mongo连接
mongo_conn = pymongo.MongoClient()

for page in range(1, 1 + 1):
    # 模拟请求
    url = "https://www.douyu.com/wgapi/ordnc/live/web/room/yzList/1"
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }
    response = requests.get(url, headers=headers)
    result = response.content.decode()
    print(response.status_code)
    print(result)

    # 数据解析 json反序列化解析需要的数据字段
    result = json.loads(result)
    for item in result.get('data').get('rl'):
        rn = item.get('rn')
        nn = item.get('nn')
        url = "https://www.douyu.com" + item.get('url')
        print(rn, nn, url)
        # mongo存储
        mongo_conn['douyu']['room'].insert_one({'rn': rn, 'nn': nn, 'url': url})
